var center = new GLatLng(0,15);
var zoom = 3;
var map;
var markersArray = [];

function initialize() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.setCenter(center, zoom);
		// GEvent.addListener(map, "click", function(overlay, latlng) {
			// alert(latlng);
        // });
		map.addControl(new GLargeMapControl());
		// var content924 = '<div style="padding: 0px 0px 8px 8px; background: url(images/point_bottom_left.png) no-repeat bottom left;"><div style="background-color: #f2efe9; padding: 2px; font-size: 0.7em;"><nobr><a href="blackbirds.html?bird=924">A-12A #60-6924</a></nobr></div></div>';

		// var label924 = new TLabel();
		// label924.id = 'label924';
		// label924.anchorLatLng = new GLatLng (0,15);
		// label924.anchorPoint = 'bottomLeft';
		// label924.content = content924;
		// label924.percentOpacity = 70;

		// map.addTLabel(label924);
	}
}

function setCenter() {
	map.setCenter(center, zoom);
}

function createMarkers(xml, color) {
	var markers = xml.documentElement.getElementsByTagName("marker");
	for (var i = 0; i < markers.length; i++) {
		var latlng = new GLatLng(parseFloat(markers[i].getAttribute("lat")),parseFloat(markers[i].getAttribute("lng")));
		var info = markers[i].getElementsByTagName("info")[0];
		map.addOverlay(createMarker(latlng,info,info,color));
	}
}

function createMarker(point,info,title,color) {
	var icon = new GIcon(G_DEFAULT_ICON);
	icon.image = "http://gmaps-samples.googlecode.com/svn/trunk/markers/"+color+"/blank.png";
	markerOptions = {icon:icon, title:title};
	var marker = new GMarker(point,markerOptions);
	if (marker) {
		if (info != '' && info != null) {
			GEvent.addListener(marker, "click", function() {
				marker.openInfoWindowHtml(info);
			});
		}
		markersArray.push(marker);
	}
	return marker;
}

function getMarkers_File(file, color) {
	if (color == null || color == '') color="red";
	GDownloadUrl(file, function(data) {
		createMarkers((GXml.parse(data)));
	});
}

function getMarkers(XML, color) {
	if (color == null || color == '') color="red";
	createMarkers((GXml.parse(XML)), color);
}

function getMarkers_AJAX(file, color) {
	if (color == null || color == '') color="red";
	var xmlHttp = GXmlHttp.create();
	xmlHttp.onreadystatechange=function() {
		if (xmlHttp.readyState==4) {
			createMarkers((GXml.parse(xmlHttp.responseText)),color);
		}
	}
	xmlHttp.open("GET",file,true);
	xmlHttp.send(null);
}

function call(file) {
	var xmlHttp = GXmlHttp.create();
	xmlHttp.onreadystatechange=function() {
		if (xmlHttp.readyState==4) {
			var code = xmlHttp.responseText;
		}
	}
	xmlHttp.open("GET",file,true);
	xmlHttp.send(null);
}

function addSpecies(file) {
	var speciesID, regionID, userID, population;
	speciesID = document.getElementById("selectSpecies")[document.getElementById("selectSpecies").selectedIndex].value;
	regionID = document.getElementById("selectRegion")[document.getElementById("selectRegion").selectedIndex].value;
	population = document.getElementById("population").value;
	params = "selectSpecies="+encodeURI(speciesID)+"&selectRegion="+encodeURI(regionID)+"&population="+encodeURI(population);
	var xmlHttp = GXmlHttp.create();
	xmlHttp.onreadystatechange=function() {
		if (xmlHttp.readyState==4) {
			var code = xmlHttp.responseText;
		}
	}
	xmlHttp.open("POST",file,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", 3);
    xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
	return false;
}

function showPopulation(file) {
	var speciesID;
	speciesID = document.getElementById("species")[document.getElementById("species").selectedIndex].value;
	params = "SpeciesID="+encodeURI(speciesID);
	var xmlHttp = GXmlHttp.create();
	xmlHttp.onreadystatechange=function() {
		if (xmlHttp.readyState==4) {
			eval(xmlHttp.responseText);
		}
	}
	xmlHttp.open("POST",file,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", 1);
    xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
	return false;
}